package array.medium;

/**
 * 209. 长度最小的子数组
 */
public class minSubArrayLen {

    public static void main(String[] args) {
        int[] test = {4,1,2,1,4};
        System.out.println(MyCode(4, test));
    }

    public static int MyCode(int target, int[] nums) {
        int p1 = 0;
        int p2 = 0;
        int sum = 0;
        int ans = Integer.MAX_VALUE;

        while (p2 < nums.length) {

            sum += nums[p2];

            while (sum >= target) {
                ans = Math.min(ans, p2 - p1 + 1);
                sum -= nums[p1];
                p1++;
            }
            p2++;
        }
        return ans == Integer.MAX_VALUE ? 0 : ans;
    }
}
